package com.xiaoq.matrix.minipro.util;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
import java.io.Serializable;

/**
 * @author <a href="https://github.com/007gzs">007</a>
 */
@XStreamAlias("xml")
public class WxOpenXmlMessage implements Serializable {
  private static final long serialVersionUID = -5641769554709507771L;

  @XStreamAlias("AppId")
  @XStreamConverter(value = XStreamCDataConverter.class)
  private String appId;

  @XStreamAlias("CreateTime")
  private Long createTime;

  @XStreamAlias("InfoType")
  @XStreamConverter(value = XStreamCDataConverter.class)
  private String infoType;

  @XStreamAlias("ComponentVerifyTicket")
  @XStreamConverter(value = XStreamCDataConverter.class)
  private String componentVerifyTicket;


  @XStreamAlias("AuthorizerAppid")
  @XStreamConverter(value = XStreamCDataConverter.class)
  private String authorizerAppid;

  @XStreamAlias("AuthorizationCode")
  @XStreamConverter(value = XStreamCDataConverter.class)
  private String authorizationCode;

  @XStreamAlias("AuthorizationCodeExpiredTime")
  private Long authorizationCodeExpiredTime;

  @XStreamAlias("PreAuthCode")
  @XStreamConverter(value = XStreamCDataConverter.class)
  private String preAuthCode;
  
  
public String getAppId() {
	return appId;
}

public void setAppId(String appId) {
	this.appId = appId;
}

public Long getCreateTime() {
	return createTime;
}

public void setCreateTime(Long createTime) {
	this.createTime = createTime;
}

public String getInfoType() {
	return infoType;
}

public void setInfoType(String infoType) {
	this.infoType = infoType;
}

public String getComponentVerifyTicket() {
	return componentVerifyTicket;
}

public void setComponentVerifyTicket(String componentVerifyTicket) {
	this.componentVerifyTicket = componentVerifyTicket;
}

public String getAuthorizerAppid() {
	return authorizerAppid;
}

public void setAuthorizerAppid(String authorizerAppid) {
	this.authorizerAppid = authorizerAppid;
}

public String getAuthorizationCode() {
	return authorizationCode;
}

public void setAuthorizationCode(String authorizationCode) {
	this.authorizationCode = authorizationCode;
}

public Long getAuthorizationCodeExpiredTime() {
	return authorizationCodeExpiredTime;
}

public void setAuthorizationCodeExpiredTime(Long authorizationCodeExpiredTime) {
	this.authorizationCodeExpiredTime = authorizationCodeExpiredTime;
}

public String getPreAuthCode() {
	return preAuthCode;
}

public void setPreAuthCode(String preAuthCode) {
	this.preAuthCode = preAuthCode;
}

//
//  public static String wxMpOutXmlMessageToEncryptedXml(WxMpXmlOutMessage message, WxOpenConfigStorage wxOpenConfigStorage){
//    String plainXml = message.toXml();
//    WxOpenCryptUtil pc = new WxOpenCryptUtil(wxOpenConfigStorage);
//    return pc.encrypt(plainXml);
//  }
//
  public static WxOpenXmlMessage fromXml(String xml) {
    //修改微信变态的消息内容格式，方便解析
    xml = xml.replace("</PicList><PicList>", "");
    return XStreamTransformer.fromXml(WxOpenXmlMessage.class, xml);
  }
//
//  public static WxOpenXmlMessage fromXml(InputStream is) {
//    return XStreamTransformer.fromXml(WxOpenXmlMessage.class, is);
//  }

  /**
   * 从加密字符串转换
   *
   * @param encryptedXml        密文
   * @param wxOpenConfigStorage 配置存储器对象
   * @param timestamp           时间戳
   * @param nonce               随机串
   * @param msgSignature        签名串
   */
  public static WxOpenXmlMessage fromEncryptedXml(String encryptedXml,
                                                  String timestamp, String nonce,
                                                  String msgSignature) {
    WxOpenCryptUtil cryptUtil = new WxOpenCryptUtil();
    String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce,
      encryptedXml);
    return fromXml(plainText);
  }

  public static WxMpXmlMessage fromEncryptedMpXml(String encryptedXml,
                                                  String timestamp, String nonce,
                                                  String msgSignature) {
    WxOpenCryptUtil cryptUtil = new WxOpenCryptUtil();
    String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce,
      encryptedXml);
    return WxMpXmlMessage.fromXml(plainText);
  }
//
//  public static WxOpenXmlMessage fromEncryptedXml(InputStream is,
//                                                  WxOpenConfigStorage wxOpenConfigStorage, String timestamp, String nonce,
//                                                  String msgSignature) {
//    try {
//      return fromEncryptedXml(IOUtils.toString(is, "UTF-8"), wxOpenConfigStorage,
//        timestamp, nonce, msgSignature);
//    } catch (IOException e) {
//      throw new RuntimeException(e);
//    }
//  }
}